/**********************************************************************************
 * 文件名称：
 * 功能说明：
 * 作者：laud（554163925@qq.com）
 * 修改记录
 *
**********************************************************************************/
#include "gsm.h"
#include "gsm_modem.h"

/* 循环发送定时器 */
static tmr_t *tmrMdmPolling = NULL;
static tmr_t *tmrMdmMonitor = NULL;

/* 定义MODEM访问接口 */
MDM_DevTypedef *gsm_modem = NULL;


///* 排队待发送AT命令 */
static MDM_CmdListCfgTypedef ready_cmd_list;

/* 临时指令收发缓冲区定义 */
static ascii mdm_cmd_sbuffer[ MDM_TX_SBUFF_SIZE ];
static ascii mdm_cmd_xbuffer[ MDM_TX_XBUFF_SIZE ];

/* 待处理的AT响应 */
static ascii mdm_rx_buffer[ MDM_RX_BUFF_SIZE_MAX ];
MDM_RspTypedef mdm_glb_rsp = { false, false, MDM_NO_STRING, 0, 0, mdm_rx_buffer };

/* 正在处理的AT指令 */
static ascii mdm_tx_buffer[ MDM_TX_BUFF_SIZE_MAX ];
static MDM_CmdTypedef mdm_glb_cmd = { 0, mdm_tx_buffer, 0, 0, NULL, SIM_UNKNOWN, false };

/* 内部函数 */
static void MDM_ClearGlobalAtRsp ( void );
static void MDM_ClearGlobalAtCmd ( void );
static void MDM_ClearAtCmdList ( void );

static void MDM_ATCmdListPolling ( uint8_t ID );
static void MDM_WorkerExecTask ( void* data );



/*
**************************************************************************
* 函数名称： MDM_IfInit
* 功能描述： 通信AT指令初始化
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/
void MDM_IfInit ( MDM_DevTypedef *user_modem )
{
    if ( user_modem )
    {
    #if GSM_MODEM_DEBUG == 1
        printf ( "\r\n[MDM STATE] uinitialized\r\n" );
    #endif  /* GSM_MODEM_DEBUG */

    gsm_modem = user_modem;

    MDM_ClearGlobalAtCmd ();
    MDM_ClearAtCmdList ();
    MDM_ClearGlobalAtRsp ();

    if ( tmrMdmPolling == NULL )
    {
        /* 注册发送定时器 */
        tmrMdmPolling = TMR_Subscribe ( true, 2, TMR_TYPE_100MS, ( tmr_procTriggerHdlr_t )MDM_ATCmdListPolling );  
    }
    }  
}

/*
**************************************************************************
* 函数名称： MDM_ClearAtCmdList
* 功能描述： 清空当前AT指令缓存列表
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/
void MDM_ClearAtCmdList ( void )
{
  uint32_t i = 0;

  ready_cmd_list.shortCmdCounter = 0;
  ready_cmd_list.shortCmdNumber = 0;
  for ( i = 0; i < MDM_SCMD_LIST_LENGTH; i++ )
  {
    ready_cmd_list.ShortCmd[ i ].CmdIndex = 0;
    ready_cmd_list.ShortCmd[ i ].At.cmd_id = 0;        
    ready_cmd_list.ShortCmd[ i ].At.timeout = 0;
    ready_cmd_list.ShortCmd[ i ].At.retry_counter = 0;
    ready_cmd_list.ShortCmd[ i ].At.handler = NULL;
    ready_cmd_list.ShortCmd[ i ].At.required_state = SIM_UNKNOWN;
    ready_cmd_list.ShortCmd[ i ].At.write_complete = false;
    ready_cmd_list.ShortCmd[ i ].At.AtBuffer = ( ascii* )&mdm_cmd_sbuffer[ i * MDM_SCMD_BLK_SIZE ]; 
  }

  ready_cmd_list.longCmdCounter = 0;
  ready_cmd_list.longCmdNumber = 0;
  for ( i = 0; i < MDM_LCMD_LIST_LENGTH; i++ )
  {
    ready_cmd_list.LongCmd[ i ].CmdIndex = 0;
    ready_cmd_list.LongCmd[ i ].At.cmd_id = 0;        
    ready_cmd_list.LongCmd[ i ].At.timeout = 0;
    ready_cmd_list.LongCmd[ i ].At.retry_counter = 0;
    ready_cmd_list.LongCmd[ i ].At.handler = NULL;
    ready_cmd_list.LongCmd[ i ].At.required_state = SIM_UNKNOWN;
    ready_cmd_list.LongCmd[ i ].At.write_complete = false;
    ready_cmd_list.LongCmd[ i ].At.AtBuffer = ( ascii* )&mdm_cmd_xbuffer[ i * MDM_LCMD_BLK_SIZE ]; 

  }
}


/* 清空当前AT指令的响应 */
static void MDM_ClearGlobalAtRsp ( void )
{
    mdm_glb_rsp.finish = false;
    mdm_glb_rsp.IsNeed = false;
    mdm_glb_rsp.Code = MDM_NO_STRING;
    mdm_glb_rsp.totalsize = 0;

    if ( mdm_glb_rsp.indicator )
    {
    memset ( ( uint8_t* )&mdm_glb_rsp.indicator[ 0 ], 0, MDM_RX_BUFF_SIZE_MAX );
    }
}

/* 清空当前待发AT指令 */
void MDM_ClearGlobalAtCmd ( void )
{
    mdm_glb_cmd.cmd_id  = 0;
    mdm_glb_cmd.timeout = 0;
    mdm_glb_cmd.retry_counter = 0;
    mdm_glb_cmd.handler = NULL;
    mdm_glb_cmd.required_state = SIM_UNKNOWN;
    mdm_glb_cmd.write_complete = false;

    if ( mdm_glb_cmd.AtBuffer )
    {
        memset ( ( uint8_t* )&mdm_glb_cmd.AtBuffer[ 0 ], 0, MDM_TX_BUFF_SIZE_MAX );
    }
}

/*
**************************************************************************
* 函数名称： MDM_ATCmdListPolling
* 功能描述： 循环发送AT指令
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/
static void MDM_ATCmdListPolling ( uint8_t ID )
{
    if ( SHORT_CMD_COUNTER || LONG_CMD_COUNTER )
    {
        /* 待发送AT队列非空，故执行任务 */
        MDM_WorkerExecTask(NULL);
    }
}

static void MDM_RetryCurrentCommand ( void )
{
    if ( gsm_modem )
    {
        gsm_modem->is_timeout = false;

        if ( mdm_glb_cmd.retry_counter > 0 )
        {
            mdm_glb_cmd.retry_counter--;

#if GSM_MODEM_DEBUG == 1 && MDM_MONITOR_DEBUG == 1
    {
    printf ( "\r\n[MDM EVENT] Current cmd %s, retry counter %d\r\n", 
    mdm_glb_cmd.AtBuffer, 
    mdm_glb_cmd.retry_counter );
    }
#endif  /* GSM_MODEM_DEBUG */

            if ( gsm_modem->serial && gsm_modem->serial->write_data && mdm_glb_cmd.AtBuffer )
            {
                int32_t length = strlen ( ( const char* )mdm_glb_cmd.AtBuffer );
                if ( length > 0 && gsm_modem->serial->write_data ( AT_Flow_Type, mdm_glb_cmd.AtBuffer, length ) == true )
                {
                    mdm_glb_cmd.write_complete = true;
                    mdm_glb_rsp.finish = false;
                }
            }   
        }  
    }
}

static void MDM_RspTimeoutHandler ( uint8_t ID )
{
#if GSM_MODEM_DEBUG == 1 && MDM_MONITOR_DEBUG == 1
    printf ( "\r\n[MDM EVENT] response timeout trigger!\r\n" );
#endif  /* GSM_MODEM_DEBUG */    
  
    if ( mdm_glb_cmd.retry_counter <= 0 )
    {
        mdm_glb_rsp.finish = true;
        mdm_glb_rsp.Code = MDM_TIME_OUT;
        if ( mdm_glb_cmd.handler )
        {
        mdm_glb_cmd.handler ( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_RspTypedef * )&mdm_glb_rsp );
        }

        if ( gsm_modem )
        {
        gsm_modem->is_timeout = false;
        gsm_modem->status = MDM_READY;    
    }
    }
    else
    {
        MDM_RetryCurrentCommand ();
    }

    if ( mdm_glb_rsp.finish == true )
    {
#if GSM_MODEM_DEBUG == 1 && MDM_MONITOR_DEBUG == 1
{
    printf ( "\r\n[MDM EVENT] monitor stop\r\n" );
}
#endif  /* GSM_MODEM_DEBUG */ 

        if ( tmrMdmMonitor )
        {
            TMR_UnSubscribe ( tmrMdmMonitor, ( tmr_procTriggerHdlr_t )MDM_RspTimeoutHandler, TMR_TYPE_100MS );
            tmrMdmMonitor = NULL;
        }
    }
}

/*
**************************************************************************
* 函数名称： MDM_WaitForCmdRsp
* 功能描述： 
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/

static void MDM_WaitForCmdRsp ( uint32_t Timeout )
{
    /* Wait for a modem operation to complete 
     or a TIMEOUT to occur */
    if ( ( mdm_glb_rsp.finish != true )&&( Timeout != 0x00 ) )
    {
    if ( tmrMdmMonitor )
    {
        TMR_UnSubscribe ( tmrMdmMonitor, ( tmr_procTriggerHdlr_t )MDM_RspTimeoutHandler, TMR_TYPE_10MS );
        tmrMdmMonitor = NULL;
    }

    tmrMdmMonitor = TMR_Subscribe ( true, Timeout, TMR_TYPE_10MS, ( tmr_procTriggerHdlr_t )MDM_RspTimeoutHandler );
    }
}

/*
**************************************************************************
* 函数名称： MDM_WorkerExecTask
* 功能描述： 任务工作组
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/
void MDM_WorkerExecTask ( void* data )
{
    if ( gsm_modem &&  gsm_modem->status < MDM_IS_BUSY && gsm_modem->status >= MDM_SERIAL_INIT )
    {
        bool found = false;
        uint32_t sendCmdIndex = 0;
        uint16_t i = 0;
        uint16_t tempCmdLoc = 0;
        
        if ( SHORT_CMD_COUNTER > 0 )
        {
            for ( ( i = 0, sendCmdIndex = 0 ); i < MDM_SCMD_LIST_LENGTH; i++ )
            {
                if ( SHORT_CMD_ID( i ) && ( SHORT_CMD_STATE( i ) <= gsm_modem->sim_status ) )
                {   

                    #if GSM_MODEM_DEBUG == 1 && MDM_AT_SEND_DEBUG == 9
                    {
                    printf ( "\r\n[MDM EVENT] get idx %u, ser %u\r\n", 
                    i, SHORT_CMD_INDEX( i ) );
                    }
                    #endif  /* GSM_MODEM_DEBUG */ 

                    if ( sendCmdIndex > SHORT_CMD_INDEX( i ) ) 
                    {
                        sendCmdIndex = SHORT_CMD_INDEX( i );
                        tempCmdLoc = i;
                    }
                    else
                        if ( found == false )
                        {
                        sendCmdIndex = SHORT_CMD_INDEX( i );
                        tempCmdLoc = i;  
                        }
                        found = true;    
                }
            }
            if ( found == true )
            {
                #if GSM_MODEM_DEBUG == 1 && MDM_AT_SEND_DEBUG == 1
                {
                printf ( "\r\n[MDM EVENT] read short cmds left %u, send idx %u, ser %u\r\n", 
                SHORT_CMD_COUNTER, 
                tempCmdLoc, 
                SHORT_CMD_INDEX( tempCmdLoc ) );
                }
                #endif  /* GSM_MODEM_DEBUG */            

                MDM_ClearGlobalAtCmd ();
                MDM_ClearGlobalAtRsp ();     
                MDM_CmdCpy ( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_CmdTypedef * )&SHORT_CMD ( tempCmdLoc ) );

                if ( gsm_modem->serial && gsm_modem->serial->write_data && mdm_glb_cmd.AtBuffer )
                {
                    uint32_t length = strlen ( ( const char* )mdm_glb_cmd.AtBuffer );

                    if ( length > 0 && gsm_modem->serial->write_data ( AT_Flow_Type, mdm_glb_cmd.AtBuffer, length ) == true )
                    {
                        SHORT_CMD_INDEX ( tempCmdLoc ) = 0;
                        MDM_CmdSet ( ( MDM_CmdTypedef * )&SHORT_CMD ( tempCmdLoc ) );
                        if ( SHORT_CMD_COUNTER > 0 )
                        {
                        SHORT_CMD_COUNTER--;
                        }

                        #if GSM_MODEM_DEBUG == 1
                        {
                        printf ( "\r\n[MDM STATE] is busy\r\n" );
                        }
                        #endif  /* GSM_MODEM_DEBUG */

                        gsm_modem->status = MDM_IS_BUSY;
                        mdm_glb_cmd.write_complete = true;
                        MDM_WaitForCmdRsp ( mdm_glb_cmd.timeout );
                    }
                    return ;
                }
            } 
        }
        if ( LONG_CMD_COUNTER > 0 )
        {
            for ( ( i = 0, sendCmdIndex = 0 ); i < MDM_LCMD_LIST_LENGTH; i++ )
            {
                if ( LONG_CMD_ID( i ) && ( LONG_CMD_STATE( i ) <= gsm_modem->sim_status ) )
                {   
                    if ( sendCmdIndex > LONG_CMD_INDEX( i ) ) 
                    {
                        sendCmdIndex = LONG_CMD_INDEX( i );
                        tempCmdLoc = i;
                    }
                    else
                        if ( found == false )
                        {
                            sendCmdIndex = LONG_CMD_INDEX( i );
                            tempCmdLoc = i;  
                        }
                        found = true;    
                }
            }

            if ( found == true )
            {
                #if GSM_MODEM_DEBUG == 1 && MDM_AT_SEND_DEBUG == 1
                {
                printf ( "\r\n[MDM EVENT] read long cmds left %u, send index %u, ser %u\r\n", 
                LONG_CMD_COUNTER, 
                tempCmdLoc, 
                LONG_CMD_INDEX( tempCmdLoc ) );
                }
                #endif  /* GSM_MODEM_DEBUG */

                MDM_ClearGlobalAtCmd ();
                MDM_ClearGlobalAtRsp ();       
                MDM_CmdCpy ( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_CmdTypedef * )&LONG_CMD ( tempCmdLoc ) );

                if ( gsm_modem->serial && gsm_modem->serial->write_data && mdm_glb_cmd.AtBuffer )
                {
                    uint32_t length = strlen ( ( const char* )mdm_glb_cmd.AtBuffer );

                    if ( length > 0 && gsm_modem->serial->write_data ( AT_Flow_Type, mdm_glb_cmd.AtBuffer, length ) == true )
                    {
                        LONG_CMD_INDEX ( tempCmdLoc ) = 0;
                        MDM_CmdSet ( ( MDM_CmdTypedef * )&LONG_CMD ( tempCmdLoc ) );
                        if ( LONG_CMD_COUNTER > 0 )
                        {
                        LONG_CMD_COUNTER--;
                        }

                        #if GSM_MODEM_DEBUG == 1
                        {
                        printf ( "\r\n[MDM STATE] is busy\r\n" );
                        }
                        #endif  /* GSM_MODEM_DEBUG */

                        gsm_modem->status = MDM_IS_BUSY;
                        mdm_glb_cmd.write_complete = true;
                        MDM_WaitForCmdRsp ( mdm_glb_cmd.timeout );
                    }
                }
            }          
        }
    }
}



/* AT指令定位器 */
extern bool MDM_PostAtCmdByID ( MDM_CmdTypedef *command, AtCommandID id, const ascii* params )
{
    if ( command &&( id >= PIN_QUERY && id <= VENDOR_COMMAND )&& gsm_modem )
    {
        while ( command->cmd_id > 0 )
        {         
            if ( command->cmd_id != id )
            {
            command++;
            }
            else
            {
                uint32_t i = 0;
                for ( i = 0; ( SHORT_CMD_ID( i ) != id )&&( i < MDM_SCMD_LIST_LENGTH ); i++ );
                
                if ( i < MDM_SCMD_LIST_LENGTH )
                {
                    MDM_CmdCpy ( ( MDM_CmdTypedef * )&SHORT_CMD( i ), command );
                    SHORT_CMD_INDEX( i ) = ++SHORT_CMD_NUMBER;
                    if ( params )
                    {
                    snprintf ( ( char* )SHORT_CMD_AT_BUFFER( i ), MDM_SCMD_BLK_SIZE, 
                    ( const char* )command->AtBuffer, params );
                    }

                    #if GSM_MODEM_DEBUG == 1 && MDM_AT_SEND_DEBUG == 1
                    {
                    printf ( "\r\n[MDM EVENT] rewrite short cmd [%u] %s, counter %u\r\n", 
                    i, 
                    SHORT_CMD_AT_BUFFER( i ), 
                    SHORT_CMD_COUNTER );
                    }
                    #endif  /* GSM_MODEM_DEBUG */
                    return true;        
                }

                for ( i = 0; LONG_CMD_ID( i ) &&( i < MDM_LCMD_LIST_LENGTH ); i++ );
                if ( i < MDM_LCMD_LIST_LENGTH )
                {
                    ++LONG_CMD_COUNTER;
                    MDM_CmdCpy ( ( MDM_CmdTypedef * )&LONG_CMD( i ), command );
                    if ( LONG_CMD_ID( i ) == RETURN_TO_DATA_STATE ) 
                    {
                    LONG_CMD_INDEX( i ) = 0xFFFFFFFF;
                    }
                    else
                    {
                    LONG_CMD_INDEX( i ) = ++LONG_CMD_NUMBER;
                    }
                    if ( params )
                    {
                    snprintf ( ( char* )LONG_CMD_AT_BUFFER( i ), MDM_LCMD_BLK_SIZE, 
                    ( const char* )command->AtBuffer, params );
                    }

                    #if GSM_MODEM_DEBUG == 1 && MDM_AT_SEND_DEBUG == 1
                    {
                    printf ( "\r\n[MDM EVENT] write long cmd [%u] %s, counter %u, ser %u\r\n", 
                    i, 
                    LONG_CMD_AT_BUFFER( i ), 
                    LONG_CMD_COUNTER, 
                    LONG_CMD_INDEX( i ) );
                    }
                    #endif  /* GSM_MODEM_DEBUG */
                    return true;
                }
                break;       
            }
        }  
    }
    return false;
}

extern bool MDM_ReadData ( uint16_t DataSize, uint8_t *Data )
{
    AtCmdHandler_Status status = AT_HANDLER_DONT_UNDERSTAND;
    AtCmdHandler handler = NULL;
    uint16_t length = 0;
    uint16_t index  = 0;
    bool result = false;
    
    if ( mdm_glb_cmd.handler )
    {
        handler = mdm_glb_cmd.handler;
    }
    
    // 清空接收缓冲区
    if(mdm_glb_rsp.IsNeed == false)
    {
        MDM_ClearGlobalAtRsp();
    }  
    
    while ( index < DataSize )     
    {
        // 1.更新数据接收处理长度 
        if ( ( mdm_glb_rsp.totalsize + DataSize - index ) < MDM_RX_BUFF_SIZE_MAX )
        {
            length = ( DataSize - index );    
        }
        else
        {
            length = ( MDM_RX_BUFF_SIZE_MAX - mdm_glb_rsp.totalsize );
        }      
        mdm_glb_rsp.handledsize = length;    
        memcpy ( ( uint8_t* )&mdm_glb_rsp.indicator[ mdm_glb_rsp.totalsize ], Data + index, length );
        mdm_glb_rsp.totalsize += length;	
        
        mdm_glb_rsp.Code = MDM_StrGetID ( ( MDM_RspTypedef * )&mdm_glb_rsp );	

        mdm_glb_rsp.finish = MDM_StrIsTerminalResponse ( mdm_glb_rsp.Code );   

        // 2.数据分析处理
        if ( handler )
        {
            status = ( *handler )( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_RspTypedef * )&mdm_glb_rsp );
            switch ( status )
            {
                case AT_HANDLER_DONE:
                    
                case AT_HANDLER_DONE_ERROR:
                    printf ( "\r\n[MDM EVENT] status AT_HANDLER_DONE\r\n" );
                    result = true;
                    gsm_modem->serial->response_unidentified( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_RspTypedef * )&mdm_glb_rsp );
                    MDM_ClearGlobalAtCmd ();
                    MDM_ClearGlobalAtRsp ();

                    if ( gsm_modem )
                    {
                        /* if handled then process next command if there is */
                        gsm_modem->status = MDM_READY;            
                    }
                break;
                
                case AT_HANDLER_ERROR:
                    printf ( "\r\n[MDM EVENT] status AT_HANDLER_ERROR\r\n" );
                    if ( gsm_modem )
                    {
                        gsm_modem->is_timeout = false;
                        gsm_modem->status = MDM_READY;            
                    }

                    /* run unsolicite because we don't know if the message
                    * belonged to handler or not */ 
                    gsm_modem->serial->response_unidentified ( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_RspTypedef * )&mdm_glb_rsp );
                    MDM_ClearGlobalAtRsp();
                break;
            
                case AT_HANDLER_NEED_MORE:
                    printf ( "\r\n[MDM EVENT] status AT_HANDLER_ERROR\r\n" );
                    mdm_glb_rsp.IsNeed = true;
                break;
                
                case AT_HANDLER_DONT_UNDERSTAND:
                    printf ( "\r\n[MDM EVENT] status AT_HANDLER_DONT_UNDERSTAND\r\n" );
                    if (gsm_modem->serial->response_unidentified ( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_RspTypedef * )&mdm_glb_rsp ) == AT_HANDLER_DONE )
                    {
                        if ( gsm_modem )
                        {
                        gsm_modem->status = MDM_READY;
                        }        
                    }
                break;   
                case AT_HANDLER_RETRY:
                    printf ( "\r\n[MDM EVENT] status AT_HANDLER_RETRY\r\n" );
                    result = true;
                break;
            }
        }
        else
        {
            if(gsm_modem->serial->response_unidentified ( ( MDM_CmdTypedef * )&mdm_glb_cmd, ( MDM_RspTypedef * )&mdm_glb_rsp ) == AT_HANDLER_DONE )
            {
                if ( gsm_modem )
                {
                    gsm_modem->status = MDM_READY;
                }
                printf ( "\r\n[MDM EVENT] status MDM_Handler_UnsRsp\r\n" );
                result = true;
            }
            if(mdm_glb_rsp.handledsize != length)
            {
                mdm_glb_rsp.totalsize = 0;
                memset ( mdm_glb_rsp.indicator, 0, MDM_RX_BUFF_SIZE_MAX );
            }

            index += mdm_glb_rsp.handledsize;
            if ( mdm_glb_rsp.totalsize >= MDM_RX_BUFF_SIZE_MAX )
            {
                mdm_glb_rsp.totalsize = 0;
                memset ( mdm_glb_rsp.indicator, 0, MDM_RX_BUFF_SIZE_MAX );
            }
        }
    }
    return result;
}


/**
 * @brief Changes sim's status
 * @param modem modem whose sim status to change
 * @param status new status to set
 */
/*
**************************************************************************
* 函数名称： MDM_ChangeSIMstatus
* 功能描述： modem modem whose sim status to change , status new status to set
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/
extern void MDM_ChangeSIMstatus ( SIM_Status status )
{
  if ( gsm_modem )
  {
    if ( ( gsm_modem->sim_status < SIM_READY )&&( status >= SIM_READY ) )
    {
				gsm_modem->sim_status = status; 
			  if(gsm_modem->sim->generalAtWithSIMReady){
						gsm_modem->sim->generalAtWithSIMReady(NULL);
				}
    }  
  }
}

/*
**************************************************************************
* 函数名称： SIM_ChangeAuthStatus
* 功能描述： Changes auth (pin) status, sends auth_status signal if
* 输入参数： 
* 输出参数： 
* 返 回 值： 
* 其它说明： 
* 修改日期        版本号     修改人	      修改内容
* -----------------------------------------------
* 2017/10/12	  	V1.1.0	     goly         XXXX
***************************************************************************
*/
void SIM_ChangeAuthStatus ( void* ipc_data, SIM_Status status, const ascii *message, bool send_reply )
{
  /* If sim status has changed, send a signal */
  if ( gsm_modem && gsm_modem->sim && gsm_modem->sim->auth_status &&( status != gsm_modem->sim_status ) )
  {
			gsm_modem->sim->auth_status ( status );
  }
  /* finally change modem's sim status */
  MDM_ChangeSIMstatus ( status );
}


